package com.hust.chatvedio;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.gson.Gson;
import com.hust.chatvedio.utils.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class AddFriendActivity extends AppCompatActivity implements View.OnClickListener, TextWatcher {
    private ProgressDialog progressDialog;
    private String message;
    private int success;
    public static String user_name;
    public static String BaseURL = Utils.BaseURL;
    public static String url_addFriend = BaseURL + "addFriend";
    private TextView mTvHeader;
    private AppCompatEditText mEdUsername;
    private MaterialButton mBtnMain;
    private ImageButton mBtnClose;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.setStatusColor(this,false,true, R.color.white);
        setContentView(R.layout.activity_add_friend);
        initView();
    }

    private void initView() {
        mTvHeader = (TextView) findViewById(R.id.tv_header);
        mEdUsername = (AppCompatEditText) findViewById(R.id.ed_username);
        mEdUsername.addTextChangedListener(this);
        mBtnMain = (MaterialButton) findViewById(R.id.btn_main);
        mBtnClose = (ImageButton) findViewById(R.id.btn_close);
        mBtnClose.setOnClickListener(this);
        mBtnMain.setOnClickListener(this);
        mBtnMain.setClickable(false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main:
                addFriend();
                break;
            case R.id.btn_close:
                finish();
                break;
        }
    }

    private void addFriend() {
        if
        (mEdUsername.getText().toString().trim().equals("")) {
            Toast.makeText(this, "请输入用户名", Toast.LENGTH_LONG).show();
        } else {
            //定义一个队列
            RequestQueue requestQueue = Volley.newRequestQueue(this);
            //发送请求
            Map<String, String> map = new HashMap<>();
            map.put("friendName", mEdUsername.getText().toString());
            map.put("userName", MainActivity.userName);
            //在进行异步任务前先显示一个 Progress Dialog
            progressDialog = new ProgressDialog(AddFriendActivity.this);
            progressDialog.setMessage("正在添加好友...");
            progressDialog.setIndeterminate(false);
            progressDialog.setCancelable(true);
            progressDialog.show();
            //定义请求
            Request request = new JsonObjectRequest(1, url_addFriend,
                    new Gson().toJson(map), new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    try {
                        success = jsonObject.getInt("success");
                        message = jsonObject.optString("message");
                        progressDialog.dismiss();
                        showDialog(AddFriendActivity.this,message);
                        mEdUsername.setText("");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    Log.d("error", "onErrorResponse: " + volleyError.toString());
                }
            });
            //将请求放入队列
            requestQueue.add(request);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if(mEdUsername.getText().toString().trim().equals("")) {
            mBtnMain.setClickable(false);
            mBtnMain.setBackgroundColor(getResources().getColor(R.color.gray_200));
            mBtnMain.setTextColor(getResources().getColor(R.color.gray_700));
        }else {
            mBtnMain.setClickable(true);
            mBtnMain.setBackgroundColor(getResources().getColor(R.color.green_700));
            mBtnMain.setTextColor(getResources().getColor(R.color.white));
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }


    private  void showDialog(Context context, String message) {
        MaterialAlertDialogBuilder materialAlertDialogBuilder = new MaterialAlertDialogBuilder(context);
        materialAlertDialogBuilder
                .setMessage(message)
                .setNegativeButton("取消",new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                    }
                })
                .setPositiveButton("确定",new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                        finish();
                    }
                });
        materialAlertDialogBuilder.show();
    }
}